<html>
<head><meta charset="utf-8"><title>conditional jmp immediately followed by unconditional jmp · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html">conditional jmp immediately followed by unconditional jmp</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203778942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203778942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203778942">(Jul 13 2020 at 22:24)</a>:</h4>
<p>Is there some reason why the compiler (with optimizations enabled) would generate code like this?</p>
<div class="codehilite"><pre><span></span><code>    0036:   48 85 f6                test   %rsi,%rsi
    0039:   0f 85 95 00 00 00       jne    00d4 &lt;_ZN3foo3foo17h3398b7961e5013deE+0xd4&gt;
    003f:   e9 9b 00 00 00          jmpq   00df &lt;_ZN3foo3foo17h3398b7961e5013deE+0xdf&gt;
</code></pre></div>



<a name="203778975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203778975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203778975">(Jul 13 2020 at 22:25)</a>:</h4>
<p>Why not generate a single <code>je</code>?</p>



<a name="203780999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203780999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203780999">(Jul 13 2020 at 22:49)</a>:</h4>
<p>What would be the best way to diagnose this, and where would be the best place to report it?</p>



<a name="203795156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203795156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203795156">(Jul 14 2020 at 03:16)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> typically with stuff like this, the thing to try and do is reproduce it in clang and report it to LLVM</p>



<a name="203795161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203795161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203795161">(Jul 14 2020 at 03:16)</a>:</h4>
<p>or at least that's my view</p>



<a name="203795172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203795172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203795172">(Jul 14 2020 at 03:17)</a>:</h4>
<p>I don't think that pattern is surprising, but I don't remember the explanation. cc <span class="user-mention" data-user-id="124289">@Hanna Kruppe</span></p>



<a name="203801498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203801498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203801498">(Jul 14 2020 at 06:06)</a>:</h4>
<p>IIUC, neither jump target is right after the block you've shown. Using a single <code>je</code> and no <code>jmp</code> only works if the target of the latter would be right after the <code>je</code>, but there can be several reasons why the blocks are not laid out that way. Most basically, if there are several blocks jumping to +0xdf, only one of them can get there by fall through (unless you want to duplicate the block), so the others will have to jump there.</p>



<a name="203808874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203808874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203808874">(Jul 14 2020 at 08:17)</a>:</h4>
<p>Ah, I see. So the likely scenario is that it wasn't trivial for LLVM to arrange for one of the blocks to follow as a fall-through? That makes sense.</p>



<a name="203832086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203832086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203832086">(Jul 14 2020 at 13:23)</a>:</h4>
<p>I don't know the rest of the specific example you were looking at, but often it's not just "not trivial" to get fall-through but fall-through would be a worse option. For example, consider</p>
<div class="codehilite"><pre><span></span><code>bb0:
    if (cond1) goto bb2 else goto bb3
bb1:
    if (cond2) goto bb2 else goto bb4
</code></pre></div>


<p>You can't have both bb0 and bb1 fall-through to bb2, unless you duplicate bb2 and that probably hurts more than it helps (for code size if bb2 is reasonably large; also for runtime performance if the respective branches are unlikely).</p>



<a name="203832632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/conditional%20jmp%20immediately%20followed%20by%20unconditional%20jmp/near/203832632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/conditional.20jmp.20immediately.20followed.20by.20unconditional.20jmp.html#203832632">(Jul 14 2020 at 13:27)</a>:</h4>
<p>And outside of these cases where you'd have to duplicate blocks, it may also be that a block order that results in more unconditional jumps than strictly necessary is still beneficial overall, e.g. due to branch probabilities. (And yes, of course there's also cases where the compiler output just isn't optimal.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>